package com.qsmx.qingqiuticket.app.util;

import java.util.HashMap;
import java.util.Map;

public class HIDKeyConverter {
    // 修饰键位掩码
    private static final int LEFT_CTRL = 0x01;
    private static final int LEFT_SHIFT = 0x02;
    private static final int LEFT_ALT = 0x04;
    private static final int LEFT_GUI = 0x08;
    private static final int RIGHT_CTRL = 0x10;
    private static final int RIGHT_SHIFT = 0x20;
    private static final int RIGHT_ALT = 0x40;
    private static final int RIGHT_GUI = 0x80;

    // HID键值到字符的映射(小写)
    private static final Map<Integer, Character> keyToCharMap = new HashMap<>();
    // Shift状态下的字符映射
    private static final Map<Integer, Character> keyToShiftCharMap = new HashMap<>();

    static {
        initializeKeyMappings();
    }

    private static void initializeKeyMappings() {
        // 字母键 (a-z)
        for (int i = 0; i < 26; i++) {
            keyToCharMap.put(0x04 + i, (char) ('a' + i));
            keyToShiftCharMap.put(0x04 + i, (char) ('A' + i));
        }

        // 数字键 (1-0)
        keyToCharMap.put(0x1E, '1'); keyToShiftCharMap.put(0x1E, '!');
        keyToCharMap.put(0x1F, '2'); keyToShiftCharMap.put(0x1F, '@');
        keyToCharMap.put(0x20, '3'); keyToShiftCharMap.put(0x20, '#');
        keyToCharMap.put(0x21, '4'); keyToShiftCharMap.put(0x21, '$');
        keyToCharMap.put(0x22, '5'); keyToShiftCharMap.put(0x22, '%');
        keyToCharMap.put(0x23, '6'); keyToShiftCharMap.put(0x23, '^');
        keyToCharMap.put(0x24, '7'); keyToShiftCharMap.put(0x24, '&');
        keyToCharMap.put(0x25, '8'); keyToShiftCharMap.put(0x25, '*');
        keyToCharMap.put(0x26, '9'); keyToShiftCharMap.put(0x26, '(');
        keyToCharMap.put(0x27, '0'); keyToShiftCharMap.put(0x27, ')');

        // 符号键
        keyToCharMap.put(0x2D, '-'); keyToShiftCharMap.put(0x2D, '_');
        keyToCharMap.put(0x2E, '='); keyToShiftCharMap.put(0x2E, '+');
        keyToCharMap.put(0x2F, '['); keyToShiftCharMap.put(0x2F, '{');
        keyToCharMap.put(0x30, ']'); keyToShiftCharMap.put(0x30, '}');
        keyToCharMap.put(0x31, '\\'); keyToShiftCharMap.put(0x31, '|');
        keyToCharMap.put(0x33, ';'); keyToShiftCharMap.put(0x33, ':');
        keyToCharMap.put(0x34, '\''); keyToShiftCharMap.put(0x34, '"');
        keyToCharMap.put(0x35, '`'); keyToShiftCharMap.put(0x35, '~');
        keyToCharMap.put(0x36, ','); keyToShiftCharMap.put(0x36, '<');
        keyToCharMap.put(0x37, '.'); keyToShiftCharMap.put(0x37, '>');
        keyToCharMap.put(0x38, '/'); keyToShiftCharMap.put(0x38, '?');

        // 特殊键
        keyToCharMap.put(0x28, '\n');  // Enter
        keyToCharMap.put(0x2C, ' ');   // Space
        keyToCharMap.put(0x2A, '\b');  // Backspace
        keyToCharMap.put(0x2B, '\t');  // Tab
    }

    /**
     * 将USB HID字节数组转换为字符串
     * @param hidData 8字节的HID报告数据
     * @return 转换后的字符串
     * @throws IllegalArgumentException 如果输入数据无效
     */
    public static String convertToString(byte[] hidData) {
        if (hidData == null || hidData.length < 8) {
            throw new IllegalArgumentException("HID数据必须是8字节长度");
        }

        int modifier = hidData[0] & 0xFF;
        boolean shiftPressed = (modifier & (LEFT_SHIFT | RIGHT_SHIFT)) != 0;

        StringBuilder result = new StringBuilder();

        // 解析6个键值位(从索引2开始)
        for (int i = 2; i < 8; i++) {
            int keyCode = hidData[i] & 0xFF;
            if (keyCode == 0) continue;  // 忽略空键值

            Character c = shiftPressed ?
                    keyToShiftCharMap.get(keyCode) : keyToCharMap.get(keyCode);

            if (c != null) {
                result.append(c);
            }
        }

        return result.toString();
    }
}
